import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawPanel extends JPanel {
	private int width, height, scale, radius;
	private BinarySearchTree binST;
	private String stringBST;

	public DrawPanel(int width, int height, BinarySearchTree binST) {
		this.scale = 100;
		this.width = width;
		this.height = height;
		this.binST = binST;
		this.stringBST = "Binary Search Tree: ";
		setPreferredSize(new Dimension(width, height));

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		radius = scale / 4;
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, width - 1, height - 1);

		g.drawString("Scale: " + this.scale + "%", width - 100, 20);
		g.drawString(stringBST, 20, 20);
		g.setFont(new Font("TimesRoman", Font.PLAIN, 40 * scale / 100));
		binST.setCurRoot();
		int n = binST.deep();
		if (n == 0) {
			return;
		} else {
			if (n == 1) {
				String value = "" + binST.getCurValue();
				g.drawOval(width / 2 - radius, height / 2 - radius, 2 * radius,
						2 * radius);
				g.drawString(value, width / 2 - 10 * scale / 100, height / 2
						+ 12 * scale / 100);
			} else {
				binST.setCurRoot();
				drawBST(width / 2, height / 2 - (n * 2 - 1) * radius, g);
			}
		}

	}

	private void drawBST(int x, int y, Graphics g) {
		int deep, newX, newY;
		newY = y + 3 * radius;
		if (binST.setCurLeft()) {
			g.setColor(Color.GREEN);
			deep = binST.deep();

			newX = (int) (x - 2 * Math.pow(2, (deep - 1)) * radius);

			g.drawLine(x, y, newX, newY);
			drawBST(newX, newY, g);
			binST.setCurParent();
		}
		if (binST.setCurRight()) {
			g.setColor(Color.GREEN);
			deep = binST.deep();

			newX = (int) (x + 2 * Math.pow(2, (deep - 1)) * radius);

			g.drawLine(x, y, newX, newY);
			drawBST(newX, newY, g);
			binST.setCurParent();
		}
		g.setColor(Color.WHITE);
		g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
		g.setColor(Color.BLACK);
		String value = "" + binST.getCurValue();
		g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
		g.drawString(value, x - 10 * scale / 100, y + 12 * scale / 100);
	}

	public void addStringBST(String stringBST) {
		this.stringBST = this.stringBST + stringBST;
		repaint();
	}

	public void clearStringBST() {
		this.stringBST = "Binary Search Tree: ";
		repaint();
	}

	public void delChar(char value) {
		String str = "Binary Search Tree: ";
		for (int i = 20; i < stringBST.length(); i++) {
			if (stringBST.charAt(i) != value) {
				str = str + stringBST.charAt(i);
			}
		}
		stringBST = str;
		repaint();
	}

	public void incScale() {
		if (this.scale == 100) {
			return;
		}
		this.scale = this.scale + 10;
		repaint();
	}

	public void decScale() {
		if (this.scale == 0) {
			return;
		}
		this.scale = this.scale - 10;
		repaint();
	}
}
